package com.jwthhealth_pub.old.diet.view.fragment.adatper;

import android.content.Context;
import android.content.res.Resources;
import android.os.Handler;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ProgressBar;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.jwthhealth_pub.R;
import com.jwthhealth_pub.old.diet.presenter.IDietPresneterComple;

/**
 * Created by Administrator on 2016/12/27.
 */
public class DietNursedAdatper extends RecyclerView.Adapter {

    private Context mContext;
    IDietPresneterComple iDietpresenter;
    private Handler handler;
    private ProgressBar progressBar;
    private boolean loading;

    public void setProgressBar(ProgressBar progressBar) {
        this.progressBar = progressBar;
    }

    public void setLoading(boolean loading) {
        this.loading = loading;
    }

    public void setHandler(Handler handler) {
        this.handler = handler;
        iDietpresenter.setHandler(handler);
    }

    public DietNursedAdatper(Context context) {
        this.mContext = context;
        iDietpresenter = new IDietPresneterComple(context);
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(mContext).inflate(R.layout.item_idet_constituation, parent, false);
        return new ViewHolder(view);

    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, final int position) {
        ViewHolder viewHolder = (ViewHolder) holder;

        Resources resources = mContext.getResources();
        switch (position) {
            case 0:
                viewHolder.title.setText(resources.getString(R.string.diet_nursed_title_one));
                viewHolder.btn.setBackground(resources.getDrawable(R.mipmap.diet_two_one));
                break;
            case 1:
                viewHolder.title.setText(resources.getString(R.string.diet_nursed_title_two));
                viewHolder.btn.setBackground(resources.getDrawable(R.mipmap.diet_two_two));
                break;
            case 2:
                viewHolder.title.setText(resources.getString(R.string.diet_nursed_title_thi));
                viewHolder.btn.setBackground(resources.getDrawable(R.mipmap.diet_two_thi));
                break;
            case 3:
                viewHolder.title.setText(resources.getString(R.string.diet_nursed_title_for));
                viewHolder.btn.setBackground(resources.getDrawable(R.mipmap.diet_two_for));
                break;
            case 4:
                viewHolder.title.setText(resources.getString(R.string.diet_nursed_title_fiv));
                viewHolder.btn.setBackground(resources.getDrawable(R.mipmap.diet_two_fiv));
                break;
            case 5:
                viewHolder.title.setText(resources.getString(R.string.diet_nursed_title_six));
                viewHolder.btn.setBackground(resources.getDrawable(R.mipmap.diet_two_six));
                break;
            case 6:
                viewHolder.title.setText(resources.getString(R.string.diet_nursed_title_sev));
                viewHolder.btn.setBackground(resources.getDrawable(R.mipmap.diet_two_sev));
                break;
            case 7:
                viewHolder.title.setText(resources.getString(R.string.diet_nursed_title_eig));
                viewHolder.btn.setBackground(resources.getDrawable(R.mipmap.diet_two_eig));
                break;
            default:break;
        }


        viewHolder.btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                if (loading) {
                    return;
                }

                switch (position) {
                    case 0:
                        iDietpresenter.requestSecInfo("2", "1");
                        break;
                    case 1:
                        iDietpresenter.requestSecInfo("2", "2");
                        break;
                    case 2:
                        iDietpresenter.requestSecInfo("2", "3");
                        break;
                    case 3:
                        iDietpresenter.requestSecInfo("2", "4");
                        break;
                    case 4:
                        iDietpresenter.requestSecInfo("2", "5");
                        break;
                    case 5:
                        iDietpresenter.requestSecInfo("2", "6");
                        break;
                    case 6:
                        iDietpresenter.requestSecInfo("2", "7");
                        break;
                    case 7:
                        iDietpresenter.requestSecInfo("2", "8");
                        break;
                    default:break;
                }

                progressBar.setVisibility(View.VISIBLE);
                loading = true;
            }
        });
    }

    @Override
    public int getItemCount() {
        return 8;
    }

    class ViewHolder extends RecyclerView.ViewHolder{

        TextView title;
        RelativeLayout btn;

        public ViewHolder(View itemView) {
            super(itemView);
            title = (TextView) itemView.findViewById(R.id.item_title);
            btn = (RelativeLayout) itemView.findViewById(R.id.btn_click);
        }
    }
}
